package com.sfilyh.servelt.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sfilyh.util.DBUtil;
import com.sfilyh.util.StringUtils;

@WebServlet("/bz-admin/addArticle")
public class addArticle extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 初始化分类
		String findSql = "select id ,cname from tb_category where type = 0 ";
		request.setAttribute("categoryList", DBUtil.queryForListMap(findSql)) ;
		
		// 修改
		String id =  request.getParameter("id");
		if(StringUtils.validate(id)){
			request.setAttribute("doType", "update");
			request.setAttribute("article", DBUtil.queryForMap("select * from tb_article where id = ? ",Integer.parseInt(id)));
		}else{
			request.setAttribute("doType", "add");
		}
		
		request.getRequestDispatcher("/WEB-INF/views/admin/addArticle.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		 request.setCharacterEncoding("utf-8");
		 String title = request.getParameter("title");
		 String content =  request.getParameter("content");
		 String category = request.getParameter("category");		 
		 String status = request.getParameter("status");
		 String ispass = request.getParameter("ispass");
		 if(ispass == null || "".equals(ispass)) ispass = "0";
		 String password = request.getParameter("password");
		 
		 Integer s = 0;
		 if(StringUtils.validate(status)) s = Integer.parseInt(status);
		 
		 String type = request.getParameter("type");
		 String id = request.getParameter("id");
		 
		 if(!StringUtils.validate(title,content,category,type)){
			 
		 }
		 
		 String errorMsg = null;
		 
		 if("add".equals(type)){			 
			 String insertSql = "insert into tb_article(author,title,content,category,ispass,password,status) values(?,?,?,?,?,?,?)";
			 DBUtil.update(insertSql,1,title,content,Integer.parseInt(category),ispass,password,s);
			 errorMsg = "发布成功";
			 
		 }else if("update".equals(type) && StringUtils.validate(id)){
			 
			 String updateSql = "update tb_article set title= ?,content = ? ,category =?,status = ? ";
			 List<Object> params = new ArrayList<Object>();
			 params.add(title);
			 params.add(content);
			 params.add(Integer.parseInt(category));
			 params.add(s);
			 
			 if(ispass.equals("1")){
				 updateSql += ",ispass=1,password=?";
				 params.add(password);
			 }else{
				 updateSql += ",ispass=null,password=null ";
			 }
			 
			 updateSql += " where id = ?";
			 params.add(Integer.parseInt(id));
			 
			 DBUtil.update(updateSql, params.toArray());
			 errorMsg = "更新成功";
		 }
		 
		 request.setAttribute("msg", errorMsg);
		 request.setAttribute("toUrl", request.getContextPath()+"/bz-admin/listArticle");
		 request.getRequestDispatcher("/WEB-INF/views/toUrl.jsp").forward(request, response);
		 
	}		
}
